<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class ProductController extends Controller
{
    public function index(Request $request)
    {
    	
    	if(!$request -> search)
    	{
    		// dd(22);
    		if(!$request -> spo_id)
	    	{

	    		$crspr_id = DB::table('cro_spo_rel') -> where('cro_id',$request -> cro_id) -> lists('crspr_id');
	    		// dd($crspr_id);
	    	}elseif($request -> spo_id)
	    	{
	    		$crspr_id = DB::table('cro_spo_rel') -> where('cro_id',$request -> cro_id) -> where('spo_id',$request -> spo_id) -> lists('crspr_id');
	    		// dd($crspr_id);
	    	}
	    	//如果有品牌id传过来
	    	if($request -> brand_id)
	    	{
	    		$data = DB::table('product') -> where('brand_id',$request -> brand_id) -> whereIn('crspr_id',$crspr_id) -> paginate(12);
	    		// dd($data);
	    		
	    	}else
	    	{
	    		
	    		//如果没有品牌id传过来
	    		$data = DB::table('product') -> whereIn('crspr_id',$crspr_id) ->paginate(12);
	    		// dd($data);
	    	}
    	}else
    	{
    		
    		$data = DB::table('product') -> where('keyword','like','%'.$request -> search.'%') -> paginate(12);
    		
    	}
    	
    	return view('home/main/product',['data'=>$data,'request'=>$request -> all()]);
    	
	}

    public function show($id)
    {
    	return view('home/main/single');
    }
}
